package cz.muni.fi.pa165.library.backend;

import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.ReaderTO;
import cz.muni.fi.pa165.library.api.ReservationService;
import cz.muni.fi.pa165.library.api.ReservationTO;
import java.util.List;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Marián Labuda
 * @version 1.0
 */
@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {

    private ReservationDAO reservationDAO;

    public void setReservationDAO(ReservationDAO reservationDAO) {
        this.reservationDAO = reservationDAO;
    }
    
    @Secured("ROLE_LIBRARIAN")
    public boolean deleteReservation(ReservationTO reservationTO) {
        return reservationDAO.deleteReservation(DTOConvertor.convertReservationTOToEntity(reservationTO));
    }

    @Secured("ROLE_LIBRARIAN")
    public List<ReservationTO> findAllReservations() {
        return DTOConvertor.convertReservationEntityListToTOList(reservationDAO.findAllReservations());
    }

    @Secured("ROLE_LIBRARIAN")
    public ReservationTO findReservationByBook(BookTO bookTO) {
        return DTOConvertor.convertReservationEntityToTO(reservationDAO.
                findReservationByBook(DTOConvertor.convertBookTOToEntity(bookTO)));
    }

    @Secured("ROLE_LIBRARIAN")
    public ReservationTO findReservationByID(Long id) {
        return DTOConvertor.convertReservationEntityToTO(reservationDAO.findReservationByID(id));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public List<ReservationTO> findReservationsByReader(ReaderTO readerTO) {
        return DTOConvertor.convertReservationEntityListToTOList(reservationDAO.
                findReservationsByReader(DTOConvertor.convertReaderTOToEntity(readerTO)));
    }

    @Secured({"ROLE_LIBRARIAN" , "ROLE_READER"})
    public ReservationTO insertReservation(ReservationTO reservationTO) {
        return DTOConvertor.convertReservationEntityToTO(reservationDAO.insertReservation(DTOConvertor.
                convertReservationTOToEntity(reservationTO)));
    }
}